package com.example.demo;

import java.util.Random;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient
@RestController
@EnableScheduling
public class SentinelApplication {

    private static final Logger log = LoggerFactory.getLogger(SentinelApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(SentinelApplication.class, args);
    }

    @GetMapping("/")
    public String index() {
        log.info("111");
        return "hello index!!";
    }

    @GetMapping("/test")
    public String test() {
        log.info("222");
        return "hello sentinel!";
    }

    @GetMapping("/hello")
    public String hello() {
        log.info("333");
        return "hello hello!";
    }

    @Scheduled(fixedRate = 100)
    public void run1() throws InterruptedException {

        try {
            RestTemplate restTemplate = new RestTemplate();
            String[] c = {"/", "/hello", "/test"};
            int num = new Random().nextInt(3);
            String notice = restTemplate.getForObject("http://localhost:8888" + c[num], String.class);
            System.out.println("http请求 " + c[num] + " 结果：" + notice);
        } catch (HttpClientErrorException e) {
            System.out.println("http客户端请求出错了！" + e);
        }

        // System.out.println(Thread.currentThread().getName() + "=====>>>>>使用fixedRate  {}" + (System.currentTimeMillis() / 1000));
    }
}
